﻿/********************************************************************************
 * CoTime. All rights reserved.
 * Author: 刘冰
 * Email : ice6@live.cn
 * Description:
 *     可以理解为可以在7个番茄中内完成的一个可控的、便于管理的任务   
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace CoTime.Model
{
    public class Task
    {
        #region [ Fields / Properties ]
        /// <summary>
        /// Unique id to identify the task 
        /// </summary>
        public String UUID { get; set; }

        /// <summary>
        /// to position the task on screen
        /// </summary>
        [XmlAttribute]
        public Int32 Id { get; set; }

        public String Title { get; set; }

        /// <summary>
        /// a series of actions to record the score
        /// </summary>
        public List<Act> Acts { get; set; }

        public Act LastAct {
            get 
            {
                return Acts.LastOrDefault<Act>();
            }
        }

        public Score Score
        {
            get
            {
                return CalulateScore();
            }
        }

        [XmlAttribute]
        public Boolean IsUnplannedAndUrgent { get; set; }

        [XmlAttribute]
        public TaskPriority Priority { get; set; }

        [XmlAttribute]
        public Boolean Done { get; set; }

        #endregion

        #region [ Constructor ]

        public Task()
        {
            this.UUID = ModelHelper.GenGuid();
            this.Acts = new List<Act>();
            this.Priority = TaskPriority.None;
        }

        public Task(Int32 id) : this()
        {
            this.Id = id;
        }

        public Task(String uuid) : this()
        {
            this.UUID = uuid;
        }

        public Task(String uuid, TaskPriority priority):this()
        {
            this.UUID = uuid;
            this.Priority = priority;
        }

        #endregion

        #region [ Operations ]

        /// <summary>
        /// 添加一个行动
        /// </summary>
        public void AddAct(ActType actType, DateTime time)
        {
            var act = new Act(actType, time);
            this.Acts.Add(act);
        }
        public void AddAct(Act act)
        {
            this.Acts.Add(act);
        }
        public void AddAct(List<Act> acts)
        {
            this.Acts.AddRange(acts);
        }
        
        #endregion

        #region [ Private Helpers ]

        private Score CalulateScore()
        {
            var score = new Score();
            if (this.Acts != null && this.Acts.Count > 0)
            {
                foreach (Act act in this.Acts)
                {
                    switch (act.ActType)
                    {
                        case ActType.AddEstiPomo:
                            score.EstiPomos++;
                            break;
                        case ActType.DelEstiPomo:
                            score.EstiPomos--;
                            break;
                        case ActType.AddPomo:
                            score.UsedPomos++;
                            break;
                        case ActType.DelPomo:
                            score.UsedPomos--;
                            break;
                        case ActType.AddAbanPomo:
                            score.AbanPomos++;
                            break;
                        case ActType.DelAbanPomo:
                            score.AbanPomos--;
                            break;
                        case ActType.AddInteInte:
                            score.InteIntes++;
                            break;
                        case ActType.DelInteInte:
                            score.InteIntes--;
                            break;
                        case ActType.AddExteInte:
                            score.ExteIntes++;
                            break;
                        case ActType.DelExteInte:
                            score.ExteIntes--;
                            break;
                    }
                }
            }
            return score;
        }

        #endregion
    }

    public class Score
    {
        #region [ Pomos / Energy ]

        /// <summary>
        /// estimated pomos
        /// </summary>
        public Int32 EstiPomos { get; set; }

        public Int32 UsedPomos { get; set; }

        public Int32 RemainingPomos
        {
            get { return this.EstiPomos - this.UsedPomos; }
        }

        public Int32 AbanPomos { get; set; }

        #endregion

        #region [ Interruption ]

        /// <summary>
        /// internal interruptions
        /// </summary>
        public Int32 InteIntes { get; set; }

        /// <summary>
        /// external interruptions
        /// </summary>
        public Int32 ExteIntes { get; set; }

        #endregion
    }

    /// <summary>
    /// 活动优先级 
    /// 1:important & urgent | Do it right now!
    /// 2:important & unurgent | Do a plan to follow it!
    /// 3:unimportant & unurgent | Get rid of it!
    /// 4:urgent & unimportant | Delegate it
    /// </summary>
    public enum TaskPriority
    { 
        None = 0,
        UrgentImportant = 1,
        NotUrgentImportant = 2,
        NotUrgentNotImportant = 3,
        UrgentNotImportant =  4
    }
}
